<?php
namespace FastPHP\Core;

class Model
{
    public function __construct()
    {
        // 数据库连接只需初始化一次
        if (!class_exists('R')) {
            require_once APP_PATH . 'FastPHP/Core/db.php';
        }
        if (!\R::testConnection()) {
            $db = require ROOT_PATH . 'config/database.php';
            // 自动拼接DSN
            $dsn = sprintf('%s:host=%s;port=%s;dbname=%s;charset=%s',
                $db['type'],
                $db['host'],
                $db['port'],
                $db['dbname'],
                $db['charset']
            );
            \R::setup($dsn, $db['username'], $db['password']);
        }
    }
} 